package com.cloud.util.page;

import java.util.List;

/**
 * 
 * 分页参数对象
 * 
 * @version 1.0
 * @date 2012-09-01
 * @author DWZ框架组
 */
public class PageInfo {

	/* 每页显示记录数 */
	private int pageSize = 10;
	/* 记录总数 */
	private int totalCount = 0;
	/* 页码长度 */
	private int toolbarLen = 10;
	/* 当前页数 */
	private int currentPage = 1;
	/* 总页数 */
	private int totalPageCount = 0;
	
	/* 得到查询记录sql语句 */
	private String listHbSql;
	/* 查询结果 */
	private List<?> resultList;
	/* 得到总记录数sql语句 */
	private String totalCountHbSQL;
	/* 所有查询结果 */
	private List<?> totalResultList;
	/* 查询结果映射实体 */
	private Class entityClass;

	/**
	 * 获取分页菜单上显示的页码范围
	 * 
	 * @return
	 */
	public String[] getPageMenuBar() {

		String[] menuBar;

		/* 不足时,全部显示 */
		if (totalPageCount < toolbarLen) {
			menuBar = new String[totalPageCount];
			for (int i = 1; i <= totalPageCount; i++) {
				menuBar[i - 1] = String.valueOf(i);
			}
			return menuBar;
		}

		int len = toolbarLen / 2;
		if (currentPage <= len) {// 前半
			menuBar = new String[toolbarLen];
			for (int i = 1; i <= toolbarLen; i++) {
				menuBar[i - 1] = String.valueOf(i);
			}
		} else if (currentPage + len > totalPageCount) {// 后半
			int end = totalPageCount - toolbarLen;
			menuBar = new String[toolbarLen];
			for (int i = toolbarLen; i > 0; i--) {
				menuBar[i - 1] = String.valueOf(end + i);
			}
		} else {
			menuBar = new String[toolbarLen];
			int begin = currentPage - len;
			for (int i = 0; i < toolbarLen; i++) {
				menuBar[i] = String.valueOf(begin + i);
			}
		}

		return menuBar;

	}

	/* ==========get/set============== */
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		if (totalCount < 1) {
			totalPageCount = 1;
			return;
		}
		totalPageCount = totalCount / pageSize;
		if (totalCount % pageSize != 0) {
			totalPageCount++;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if (pageSize < 1) {
			pageSize = 5;
		}
		this.pageSize = pageSize;
	}

	public int getToolbarLen() {
		return toolbarLen;
	}

	public void setToolbarLen(int toolbarLen) {
		if (toolbarLen < 1) {
			toolbarLen = 10;
		}
		this.toolbarLen = toolbarLen;
	}

	public int getTotalPageCount() {
		return totalPageCount;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		if (currentPage < 1) {
			currentPage = 1;
		}
		this.currentPage = currentPage;
	}

	public String getTotalCountHbSQL() {
		return totalCountHbSQL;
	}

	public void setTotalCountHbSQL(String totalCountHbSQL) {
		this.totalCountHbSQL = totalCountHbSQL;
	}

	public String getListHbSql() {
		return listHbSql;
	}

	public void setListHbSql(String listHbSql) {
		this.listHbSql = listHbSql;
	}

	public List<?> getResultList() {
		return resultList;
	}

	public void setResultList(List<?> resultList) {
		this.resultList = resultList;
	}

	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public Class getEntityClass() {
		return entityClass;
	}

	public void setEntityClass(Class entityClass) {
		this.entityClass = entityClass;
	}

}
